package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

//Esse é o tag handler (um manipulador de tags) ele herda body tag support, que é uma tag que pode conter corpo.
public class FieldValidationTag extends BodyTagSupport{

	private String fieldId;
	private String required;
	private String requiredMessage;
	private String email;
	private String emailMessage;
	private String remote;
	private String remoteMessage;
	private Integer minlength;
	private String minlengthMessage;
	private Integer maxlength;
	private String maxlengthMessage;
	private String rangelength;
	private String rangelengthMessage;
	private Integer min;
	private String minMessage;
	private Integer max;
	private String maxMessage;
	private String range;
	private String rangeMessage;
	private String url;
	private String urlMessage;
	private String date;
	private String dateMessage;
	private String dateISO;
	private String dateISOMessage;
	private String dateITA;
	private String dateITAMessage;
	private String number;
	private String numberMessage;
	private String digits;
	private String digitsMessage;
	private String creditcard;
	private String creditcardMessage;
	private String equalTo;
	private String equalToMessage;
	private String cnpjValidator;
	private String cnpjValidatorMessage;
	private String cpfValidator;
	private String cpfValidatorMessage;
	private String urlShort;
	private String urlShortMessage;
	
	@Override
	public int doEndTag() throws JspException {
		
		//é o cuspe da pagina em jsp
		JspWriter saida= pageContext.getOut();
		
		//Pega o corpo
		String corpo = getBodyContent().getString();
		
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		//wraper utilizado para "capturar" o response enviado ao servidor e manipula-lo
		ResponseWrapper responseWrapper = new ResponseWrapper(response);
		//utiliza a string que na verdade é a pagina jsp completa 
		responseWrapper.getString();
		
		//mostra o caminho que a requisição será despachada
		RequestDispatcher req = request.getRequestDispatcher("/resources/componentes-jsp/fieldValidation.jsp");
	

		//atributo para ser usado na pagina jsp
		request.setAttribute("fieldId", fieldId);
		request.setAttribute("corpo", corpo);

		request.setAttribute("required", required);
		request.setAttribute("requiredMessage", requiredMessage);
		request.setAttribute("email", email);
		request.setAttribute("emailMessage", emailMessage);
		request.setAttribute("remote", remote);
		request.setAttribute("remoteMessage", remoteMessage);
		request.setAttribute("minlength", minlength);
		request.setAttribute("minlengthMessage", minlengthMessage);
		request.setAttribute("maxlength", maxlength);
		request.setAttribute("maxlengthMessage", maxlengthMessage);
		request.setAttribute("rangelength", rangelength);
		request.setAttribute("rangelengthMessage", rangelengthMessage);
		request.setAttribute("max", max);
		request.setAttribute("maxMessage", maxMessage);
		request.setAttribute("min", min);
		request.setAttribute("minMessage", minMessage);
		request.setAttribute("range", range);
		request.setAttribute("rangeMessage", rangeMessage);
		request.setAttribute("url", url);
		request.setAttribute("urlMessage", urlMessage);
		request.setAttribute("date", date);
		request.setAttribute("dateMessage", dateMessage);
		request.setAttribute("dateISO", dateISO);
		request.setAttribute("dateISOMessage", dateISOMessage);
		request.setAttribute("dateITA", dateITA);
		request.setAttribute("dateITAMessage", dateITAMessage);
		request.setAttribute("number", number);
		request.setAttribute("numberMessage",  numberMessage);
		request.setAttribute("digits", digits);
		request.setAttribute("digitsMessage", digitsMessage);
		request.setAttribute("creditcard", creditcard);
		request.setAttribute("creditcardMessage", creditcardMessage);
		request.setAttribute("equalTo", equalTo);
		request.setAttribute("equalToMessage", equalToMessage);
		request.setAttribute("cnpjValidator", cnpjValidator);
		request.setAttribute("cnpjValidatorMessage", cnpjValidatorMessage);
		request.setAttribute("cpfValidator", cpfValidator);
		request.setAttribute("cpfValidatorMessage", cpfValidatorMessage);
		request.setAttribute("urlShort", urlShort);
		request.setAttribute("urlShortMessage", urlShortMessage);

		
		try {
			//faz a mesma função do forward, so que em vez de dar o request e o response ele apenas empresta, podendo fazer alterações apos enviar a requisição
			req.include(request, responseWrapper);
			//daqui pra baixo a jsp ja esta interpretada 
			String conteudoComponente = responseWrapper.getString();
			//printa o conteudo jsp na tela
			saida.println(conteudoComponente);
			
		} catch (ServletException e) {
			e.printStackTrace();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return super.doEndTag();
	}

	public String getFieldId() {
		return fieldId;
	}

	public void setFieldId(String fieldId) {
		this.fieldId = fieldId;
	}

	public String getRequiredMessage() {
		return requiredMessage;
	}

	public void setRequiredMessage(String requiredMessage) {
		this.requiredMessage = requiredMessage;
	}

	public String getEmailMessage() {
		return emailMessage;
	}

	public void setEmailMessage(String emailMessage) {
		this.emailMessage = emailMessage;
	}

	public String getRemote() {
		return remote;
	}

	public void setRemote(String remote) {
		this.remote = remote;
	}

	public String getRemoteMessage() {
		return remoteMessage;
	}

	public void setRemoteMessage(String remoteMessage) {
		this.remoteMessage = remoteMessage;
	}

	public Integer getMinlength() {
		return minlength;
	}

	public void setMinlength(Integer minlength) {
		this.minlength = minlength;
	}

	public String getMinlengthMessage() {
		return minlengthMessage;
	}

	public void setMinlengthMessage(String minlengthMessage) {
		this.minlengthMessage = minlengthMessage;
	}

	public Integer getMaxlength() {
		return maxlength;
	}

	public void setMaxlength(Integer maxlength) {
		this.maxlength = maxlength;
	}

	public String getMaxlengthMessage() {
		return maxlengthMessage;
	}

	public void setMaxlengthMessage(String maxlengthMessage) {
		this.maxlengthMessage = maxlengthMessage;
	}

	public String getRangelengthMessage() {
		return rangelengthMessage;
	}

	public void setRangelengthMessage(String rangelengthMessage) {
		this.rangelengthMessage = rangelengthMessage;
	}

	public Integer getMin() {
		return min;
	}

	public void setMin(Integer min) {
		this.min = min;
	}

	public String getMinMessage() {
		return minMessage;
	}

	public void setMinMessage(String minMessage) {
		this.minMessage = minMessage;
	}

	public Integer getMax() {
		return max;
	}

	public void setMax(Integer max) {
		this.max = max;
	}

	public String getMaxMessage() {
		return maxMessage;
	}

	public void setMaxMessage(String maxMessage) {
		this.maxMessage = maxMessage;
	}

	public String getRange() {
		return range;
	}

	public void setRange(String range) {
		this.range = range;
	}

	public String getRangeMessage() {
		return rangeMessage;
	}

	public void setRangeMessage(String rangeMessage) {
		this.rangeMessage = rangeMessage;
	}

	public String getUrlMessage() {
		return urlMessage;
	}

	public void setUrlMessage(String urlMessage) {
		this.urlMessage = urlMessage;
	}
	
	public String getDateMessage() {
		return dateMessage;
	}

	public void setDateMessage(String dateMessage) {
		this.dateMessage = dateMessage;
	}

	public String getDateISOMessage() {
		return dateISOMessage;
	}

	public void setDateISOMessage(String dateISOMessage) {
		this.dateISOMessage = dateISOMessage;
	}

	public String getDateITAMessage() {
		return dateITAMessage;
	}

	public void setDateITAMessage(String dateITAMessage) {
		this.dateITAMessage = dateITAMessage;
	}

	public String getNumberMessage() {
		return numberMessage;
	}

	public void setNumberMessage(String numberMessage) {
		this.numberMessage = numberMessage;
	}

	public String getDigitsMessage() {
		return digitsMessage;
	}

	public void setDigitsMessage(String digitsMessage) {
		this.digitsMessage = digitsMessage;
	}
	
	public String getCreditcardMessage() {
		return creditcardMessage;
	}

	public void setCreditcardMessage(String creditcardMessage) {
		this.creditcardMessage = creditcardMessage;
	}

	public String getEqualTo() {
		return equalTo;
	}

	public void setEqualTo(String equalTo) {
		this.equalTo = equalTo;
	}

	public String getEqualToMessage() {
		return equalToMessage;
	}

	public void setEqualToMessage(String equalToMessage) {
		this.equalToMessage = equalToMessage;
	}

	public String getCnpjValidatorMessage() {
		return cnpjValidatorMessage;
	}

	public void setCnpjValidatorMessage(String cnpjValidatorMessage) {
		this.cnpjValidatorMessage = cnpjValidatorMessage;
	}

	public String getCpfValidatorMessage() {
		return cpfValidatorMessage;
	}

	public void setCpfValidatorMessage(String cpfValidatorMessage) {
		this.cpfValidatorMessage = cpfValidatorMessage;
	}

	public String getUrlShortMessage() {
		return urlShortMessage;
	}

	public void setUrlShortMessage(String urlShortMessage) {
		this.urlShortMessage = urlShortMessage;
	}

	public String getRequired() {
		return required;
	}

	public void setRequired(String required) {
		this.required = required;
	}
	
	public String getRangelength() {
		return rangelength;
	}

	public void setRangelength(String rangelength) {
		this.rangelength = rangelength;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDateISO() {
		return dateISO;
	}

	public void setDateISO(String dateISO) {
		this.dateISO = dateISO;
	}

	public String getDateITA() {
		return dateITA;
	}

	public void setDateITA(String dateITA) {
		this.dateITA = dateITA;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getDigits() {
		return digits;
	}

	public void setDigits(String digits) {
		this.digits = digits;
	}

	public String getCreditcard() {
		return creditcard;
	}

	public void setCreditcard(String creditcard) {
		this.creditcard = creditcard;
	}

	public String getCnpjValidator() {
		return cnpjValidator;
	}

	public void setCnpjValidator(String cnpjValidator) {
		this.cnpjValidator = cnpjValidator;
	}

	public String getCpfValidator() {
		return cpfValidator;
	}

	public void setCpfValidator(String cpfValidator) {
		this.cpfValidator = cpfValidator;
	}

	public String getUrlShort() {
		return urlShort;
	}

	public void setUrlShort(String urlShort) {
		this.urlShort = urlShort;
	}

	
	

	
	

}
